package com.blog.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.blog.domain.DTO.AddCategoryDto;
import com.blog.domain.DTO.CategoryListDto;
import com.blog.domain.DTO.TagListDto;
import com.blog.domain.ResponseResult;
import com.blog.domain.entity.Category;
import com.blog.domain.vo.CategoryVo;
import com.blog.domain.vo.ExcelCategoryVo;
import com.blog.domain.vo.TagVo;
import com.blog.enums.AppHttpCodeEnum;
import com.blog.service.CategoryService;
import com.blog.utils.BeanCopyUtils;
import com.blog.utils.WebUtils;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {

	@Autowired
	private CategoryService categoryService;


	/**查询所有分类
	 *
	 * @return
	 */
	@GetMapping("/listAllCategory")
	public ResponseResult<CategoryVo> selectAllCategory(){

		return categoryService.selectAllCategory();
	}


	/**数据导出
	 *
	 */
	@PreAuthorize("@ps.hasPermission('content:category:export')")
	@GetMapping("/export")
	public void export(HttpServletResponse response){
		try {
			//设置下载文件的请求头
			WebUtils.setDownLoadHeader("分类.xlsx",response);
			//获取需要导出的数据
			List<Category> categoryVos =  categoryService.list();
			List<ExcelCategoryVo> excelCategoryVos =
					BeanCopyUtils.copyBeanList(categoryVos, ExcelCategoryVo.class);
			//把数据写入到Excel中
			EasyExcel.write(response.getOutputStream(),
							ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
					.doWrite(excelCategoryVos);
		} catch (Exception e) {
			//如果出现异常也要响应json
			ResponseResult result =
					ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
			WebUtils.renderString(response, JSON.toJSONString(result));
		}
	}

	/**分类分页查询
	 *
	 * @param pageNum
	 * @param pageSize
	 * @param categoryListDto
	 * @return
	 */

	@GetMapping("/list")
	public ResponseResult categoryList(Integer pageNum, Integer pageSize
	, CategoryListDto categoryListDto){

		return categoryService.categoryList(pageNum,pageSize,categoryListDto);
	}

	/**新增分类
	 *
	 * @param addCategoryDto
	 * @return
	 */
	@PostMapping
	public ResponseResult addCategory(@RequestBody AddCategoryDto addCategoryDto){

		return categoryService.addCategory(addCategoryDto);
	}

	/**根据id查询分类回显
	 *
	 * @param id
	 * @return
	 */

	@GetMapping("/{id}")
	public ResponseResult getCategoryById(@PathVariable Long id){

		return categoryService.getCategoryById(id);
	}

	/**更新分类信息
	 *
	 * @param categoryVo
	 * @return
	 */
	@PutMapping
	public ResponseResult updateCategory(@RequestBody CategoryVo categoryVo){

		return categoryService.updateCategory(categoryVo);

	}


	/**删除分类
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/{id}")
	public  ResponseResult deletecategory(@PathVariable("id") List<Long> ids){

		return categoryService.deletecategory(ids);
	}



}
